package com.github.pig.admin.server;


import com.xiaoleilu.hutool.thread.ThreadUtil;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import javax.annotation.PostConstruct;
import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.concurrent.Semaphore;

/**
 * 调用微信支付接口-更新支付状态
 * @author: lht
 * @date: 2016/5/10
 * @version: 1.0
 */
@Service
public class VxServer extends AbstractServer {
    private static org.slf4j.Logger logger = LoggerFactory.getLogger(VxServer.class);
    public static volatile Semaphore semaphore;
    public static int count = 1;

    @PostConstruct
    public void init() {
//        super.init();
    }

    @Override
    public String getName() {
        return "VX_PAY_SERVER";
    }

    @Override
    public void run() {
            semaphore = new Semaphore(this.getPoolSize());

            while (run) {
                try {
                    ThreadUtil.sleep(time);
                    //获取通行凭证
                    semaphore.acquire();
                    //调用微信接口获取支付状态
                    System.out.println("----------微信支付状态获取开始----------------"+count++);
                    pool.submit(new VxUpdateStatusTask());
                    //释放凭证
                    semaphore.release();
                }catch (InterruptedException ie){
                    ie.printStackTrace();
                    semaphore.release();
                }catch (Exception e){
                    e.printStackTrace();
                    logger.error(getName()+"异常，原因："+e.getMessage());
                }
            }
            pool.shutdown();
    }
}
